import { useReactive } from "ahooks";
import { useModel } from "umi";
import compress from "gql-compress";
import { useEffect } from "react";

const useAllProxyServers = () => {
    const { graphqlFetchPlatform } = useModel("global");

    const state = useReactive({
        loading: false,
        proxyServers: [],
    });

    const loadAllProxyServers = async () => {
        state.loading = true;
        let res = await graphqlFetchPlatform({
            query,
            variables: {},
        });
        state.proxyServers = res?.a?.nodes || [];
        state.loading = false;
        return res?.a?.nodes || [];
    };

    useEffect(() => {
        loadAllProxyServers();
    }, []);

    return { ...state, loadAllProxyServers };
};

const query = compress(`query MyQuery {
    a: allProxyServers {
        nodes {
            proxyUuid
            createTime
            disabled
            proxyTitle
            proxyUrl
            runningStatus
            updateTime
        }
    }
}`);

export default useAllProxyServers;
